//
//  HomeViewController.m
//  Cheirapsis
//
//  Created by wangning on 15/11/2.
//  Copyright © 2015年 mohe. All rights reserved.
//

#import "HomeViewController.h"
#import "CityChooseViewController.h"
#import "StoreViewController.h"
#import "TimeChooseViewController.h"
#import "FillOutViewController.h"
#import "UIImageView+WebCache.h"
#import "MBProgressHUD.h"
#import "Define.h"
#import "PageView.h"
#import "Request.h"

@interface HomeViewController ()<PageViewDelegate>{
    NSArray *imageArray;
}

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadData];
    if ([[NSUserDefaults standardUserDefaults] valueForKey:@"city"] == nil) {
        [self setNavBar:@"北京"];
    }else{
        [self setNavBar:[[NSUserDefaults standardUserDefaults] valueForKey:@"city"]];
    }
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getCity:) name:@"getCity" object:nil];

    self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
}

- (void)viewWillAppear:(BOOL)animated{
    delegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];;
    [delegate.tabBar showTabBar];
    
}

- (void)loadData{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [[Request sharedInstance] getAdsPic:nil content:^(NSArray *arr) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        if (arr) {
            imageArray = [[NSArray alloc] init];
            imageArray = arr;
            [_tableView reloadData];
        }
    }];
    [[Request sharedInstance] getStoreName:nil content:^(NSArray *arr) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        if (arr) {
            _dataSource = arr;
            [_tableView reloadData];
        }
    }];
}

#pragma -mark 城市选择
- (void)getCity:(NSNotification *)noti{
    NSString *strCity = [noti object];
    [self setNavBar:strCity];
    [self.tableView reloadData];
}

- (void)setNavBar:(NSString *)city{
    UIButton *cityBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30*F_WIDTH, 30*F_HEIGHT)];
    [cityBtn setTitle:city forState:UIControlStateNormal];
    [cityBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    cityBtn.titleLabel.font = [UIFont systemFontOfSize:12*F_WIDTH];
    
    [cityBtn addTarget:self action:@selector(cityChoose) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *cityBar = [[UIBarButtonItem alloc] initWithCustomView:cityBtn];
    self.navigationItem.leftBarButtonItem = cityBar;
}

- (void)cityChoose{
    [self.navigationController pushViewController:[[CityChooseViewController alloc] init] animated:YES];
}

#pragma -mark setCell
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *identifier = [NSString stringWithFormat:@"identifier%ld",(long)indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    NSDictionary *dic = [_dataSource objectAtIndex:indexPath.row];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(15*F_WIDTH, 10*F_HEIGHT, 80*F_WIDTH, 80*F_HEIGHT)];
    [imageView sd_setImageWithURL:[NSURL URLWithString:dic[@"storeImageArr"][0]]];
    imageView.layer.masksToBounds = YES;
    imageView.layer.cornerRadius = 10*F_WIDTH;
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(100*F_WIDTH, 10*F_HEIGHT, 200*F_WIDTH, 15*F_HEIGHT)];
    titleLabel.text = dic[@"storeName"];
    
    UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(100*F_WIDTH, 25*F_HEIGHT, 200*F_WIDTH, 40*F_HEIGHT)];
    contentLabel.text = dic[@"storeDescripition"];
    contentLabel.font = [UIFont systemFontOfSize:12*F_WIDTH];
    contentLabel.numberOfLines = 0;
    
    UILabel *AddNumber = [[UILabel alloc] initWithFrame:CGRectMake(100*F_WIDTH, 80*F_HEIGHT, 100*F_WIDTH, 15*F_HEIGHT)];
    AddNumber.text = dic[@"storeAddress"];
    AddNumber.font = [UIFont systemFontOfSize:12*F_WIDTH];
    
    UILabel *enterLable = [[UILabel alloc] initWithFrame:CGRectMake(250*F_WIDTH, 80*F_HEIGHT, 50*F_WIDTH, 15*F_HEIGHT)];
    enterLable.text = @"点击进入";
    enterLable.backgroundColor = [UIColor colorWithRed:159/255.0 green:203/255.0 blue:239/255.0 alpha:1.0];
    enterLable.font = [UIFont systemFontOfSize:10*F_WIDTH];
    
    [cell.contentView addSubview:imageView];
    [cell.contentView addSubview:titleLabel];
    [cell.contentView addSubview:contentLabel];
    [cell.contentView addSubview:AddNumber];
    [cell.contentView addSubview:enterLable];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100*F_HEIGHT;
}

#pragma -mark setHeader
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 150*F_HEIGHT)];
    //设定广告页 Begin
    PageView *pageView = [[PageView alloc] initPageViewFrame:CGRectMake(0, 0, self.view.bounds.size.width, 150*F_HEIGHT)];
    pageView.delegate = self;
    pageView.isWebImage = YES;
    pageView.imageArray = imageArray;
    pageView.duration = 3.0;
    
    [headerView addSubview:pageView];
    //设定广告页 End
    
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 150*F_HEIGHT;
}

- (void)didSelectPageViewWithNumber:(NSInteger)selectNumber{
    if (selectNumber == 0) {
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"userName"];
        NSLog(@"进入第一个广告页面");
    }else if (selectNumber == 1){
        NSLog(@"进入第二个广告页面");
    }else if (selectNumber == 2){
        NSLog(@"进入第三个广告页面");
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic = [_dataSource objectAtIndex:indexPath.row];
    StoreViewController *store = [[StoreViewController alloc] init];
    store.dic = dic;
    [self.navigationController pushViewController:store animated:YES];
}

@end
